<template>
  <el-dialog :close-on-click-modal="false" 
    :title="title"
    :visible="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="客户名称" prop="CustomName">
        <el-input v-model.trim="form.CustomName" />
      </el-form-item>
      <el-form-item label="地址" prop="Address">
        <el-input v-model.trim="form.Address" />
      </el-form-item>
      <el-form-item label="联系人" prop="Contacts">
        <el-input v-model.trim="form.Contacts" />
      </el-form-item>
      <el-form-item label="联系方式" prop="ContactInformation">
        <el-input v-model.trim="form.ContactInformation" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { CRenew, CNewly } from '@/api/userManagement'
  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          CustomName: '',
          Address: '',
          Contacts: '',
          ContactInformation: '',
        },
        rules: {
          CustomName: [
            { required: true, trigger: 'blur', message: '请输入客户名称' },
          ],
          Address: [{ required: true, trigger: 'blur', message: '请输入地址' }],
          Contacts: [
            { required: true, trigger: 'blur', message: '请输入联系人' },
          ],
          ContactInformation: [
            { required: true, trigger: 'blur', message: '请输入联系方式' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    mounted() {
      this.getRoleInfo()
    },
    methods: {
      getRoleInfo() {
        RoleManagerInfo({ Index: 0, Count: 100 }).then((res) => {
          this.RoleInfo = res.RoleInfo
        })
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        // this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if ((this.title = '编辑')) {
              await CRenew(this.form)
            } else {
              this.form.CustomId = 'UID' + new Date().getTime()
              await CNewly(this.form)
            }
            this.$baseMessage('操作成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
